<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>{$basic.title}</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<link href="__LAYUI__/css/layui.css" rel="stylesheet">
    <link href="__LAYUI__/css/ah.css" rel="stylesheet">
</head>
<body>
<div class="wrapper-content">
<div class="ibox">
	<div class="ibox-title"><h3>用户列表</h3>
	<div class="ibox-tools">
					<div class="layui-btn-group" id="amTools">
						{:getTool(['re','add','dels'],$toolUrl)}
					  </div>            
		</div>
	
	</div>
	<div class="ibox-content">
		<table class="layui-table" lay-data="{'url':'{$dataUrl}','id':'tabData','page':true}" lay-filter="tabData">
		  <thead>
			<tr>
			<th lay-data="{type:'checkbox'}">ID</th>
			  <th lay-data="{field:'id',sort: true}">ID</th>
			  <th lay-data="{field:'username',sort: true}">用户名</th>
				<th lay-data="{field:'tel',edit:'text'}">电话</th>
			  <th lay-data="{field:'email',edit:'text',sort: true}">email</th>
			  <th lay-data="{field:'admin',sort: true}">是否管理员</th>
			  <th lay-data="{fixed: 'right', width:178, align:'center', toolbar: '#barTool'}"></th>
			</tr>
		  </thead>
		</table>
	</div>
</div>
</div>
</body>
<script type="text/html" id="barTool">
 
    <a class="layui-btn layui-btn-xs {{#  if(d.id == 1 || d.admin == 0){ }}layui-btn-disabled{{#}}}" data-url="{:url('auth/edit')}?id={{d.id}}" lay-event="auth">权限</a>

  <a class="layui-btn layui-btn-xs" data-url="{:url('user/edit')}?id={{d.id}}" lay-event="edit">编辑</a>
  <a class="layui-btn layui-btn-xs {{#  if(d.id == 1){ }}layui-btn-disabled{{#}}}" data-url="{:url('user/del')}" lay-event="del">删除</a>
</script>
<script src="__LAYUI__/layui.js"></script>
<script>
layui.use(['jquery', 'table','form', 'layer'], function(){
		
	  var $ = layui.jquery;
	  var table = layui.table;
	  var form = layui.form;
	  var layer = layui.layer;
	  var xaoam ={
		  tool:function(){
			  $('#amTools [type="button"]').on('click',function(ele){
				  	let url = $(this).data('url');
					if($(this).hasClass('am_re')){
						table.reload('tabData');
					}
					if($(this).hasClass('am_add')){
						xaoam.newWin($(this).data('url'),'');
					}
					if($(this).hasClass('am_dels')){
					  let data = table.checkStatus('tabData').data;
					  let ids = [];
						console.log(data);
							if(data.length == 0){
								layer.msg('选点什么吧!');
							}else{
							$.each(data,function(k,row){
										ids.push(row.id);
									});
								$.ajax({
									url:url,
									data:{'ids':ids},
									type:'POST',
									dataType:'JSON',
									success:function(res){
										layer.msg(res.msg);
										if(res.code == 200){
											table.reload('tabData');
										}
									}
								})
								}
							}
			  });
			  /*编辑*/
				table.on('edit(tabData)', function(obj){ 
				  let id = obj.data.id;
				  let field = obj.field;
				  let val = obj.value;
					$.ajax({
						url:"{:url('user/cue')}",
						data:{'id':id,'field':field,'value':val},
						type:'post',
						dataType:'json',
						success:function(res){
							layer.msg(res);
						}
					})
				});
			  //监听工具条*/
			  table.on('tool(tabData)', function(obj){
				let data = obj.data;
				let url = $(this).data('url');
					if($(this).hasClass('layui-btn-disabled')){
						return false;
					}
				  switch(obj.event){
				    case 'auth':
				      url = url + '&user=' + data.username;
				      xaoam.newWin(url,'');
				    break;
				    case 'del':
					  layer.confirm('真的删除行么', function(index){
							xaoam.del(obj,url);
							layer.close(index);
					  });
				    break;
				    case 'edit':
				      xaoam.newWin(url,'');
				    break;
				  };
			  });
		  },
		  del:function(obj,url){
				let data = obj.data;
				if(data.id == {$user_info.id}){
					layer.msg('当前用户登陆状态,无法删除!');
				}else{	
						  $.get(url,{'id':data.id},function(res){
							 if(res.code == 200){
								 obj.del();
								 layer.msg(res.msg);
								}
								else{
									layer.msg(res.msg);
								}
						  });

			  }
		  },
		  newWin:function(url,text){
			 var index = layer.open({ type: 2,
			  				  title: [text, 'font-size:18px;'],
			  				  content: url,
			  				});
			  		layer.full(index);
		  },
		  init:function(){
			  table.reload('tabData');
		  }
	  } 

		xaoam.tool();
		window.Wxaoam = xaoam;
	});
</script>

</html>
